﻿@using WebPortal;
@model  Model.ETL_Source_ViewConfigInfo
@{
    ViewBag.Title = "配置数据源表信息";
    Layout = "~/Views/Shared/_Main.cshtml";
}
@section Head{
    <style>
        .tb1
        {
            width: 160px;
            border: 1px;
            background: #99BBE8;
            line-height: 25px;
            font-weight: bold;
            color: #ffffff;
            padding-left: 10px;
            cursor: pointer;
        }
        .dbItem
        {
            background-color: #ffffff;
            border: #ccc 1px solid;
            height: 25px;
            padding-left: 3px;
            padding-top: 8px;
            display: none;
        }
        .tb2
        {
            width: 160px;
            border: 1px;
            background: #eee;
            line-height: 25px;
            padding-left: 10px;
            cursor: pointer;
        }
        .pagTable td
        {
            padding-left: 3px;
        }
        #ViewSreach
        {
            display: none;
            width: 500px;
            height: 330px;
            position: absolute;
            top: 34px;
            left: 150px;
            z-index: 1002;
        }
        #sarea
        {
            background-color: #ffffff;
            border: #ccc 1px solid;
            height: 310px;
            overflow-y: scroll;
        }
        .isBigInit
        {
            display: none;
        }
        #ViewInit
        {
            display: none;
            width: 500px;
            position: absolute;
            top: 110px;
            left: 150px;
            z-index: 1002;
        }
        .borderInit
        {
            background-color: #ffffff;
            border: 1px solid #99BBE8;
            height: 200px;
            overflow-y: scroll;
        }
    </style>
    <script type="text/javascript">
        var UrlArray = $.getUrlArray();
        var DBID = "";
        $(function () {
            if ('@Model.IsFile' == '1') {
                $("#div_TableMode").hide();
                $(".isfileDB").hide();
            }
            var dbType = UrlArray["DBType"];
            if (dbType != "2") {
                $("input[name='TableMode']").bind("click", function (i, d) {
                    if (this.value == 1) {
                        $("input[name='Incremental']").removeAttr("checked");
                        $("input[name='Incremental']").attr("disabled", "disabled");
                        $("input[name='ColumnIsBigDataTag']").removeAttr("checked");
                        $("input[name='ColumnIsBigDataTag']").attr("disabled", "disabled");
                        $("input[name='BigDataTagValue']").attr("disabled", "disabled");
                    } else {
                        $("input[name='Incremental']").removeAttr("disabled");
                        $("input[name='ColumnIsBigDataTag']").removeAttr("disabled");
                        $("input[name='BigDataTagValue']").removeAttr("disabled", "disabled");
                    }
                });
            } else {
                $(".dbItem").show();
            }
            initSourceViewColumn("");
        });
        function ReloadViewColumn() {
            initSourceViewColumn("reLoad");
        }
        function initSourceViewColumn(type) {
            $(".loading").show();
            var dbType = UrlArray["DBType"];
            if (dbType == "2") {
                $(".isBig").hide();
                $(".isBigInit").show();

                if ('@Model.TableMode' == '0' && $("input[name='Incremental']:checked").length == 0) {
                    $("#Statusradio1").attr("checked", "checked");
                }
                $.post("/SystemConfiguration/GetIncrementRole/", { viewID: UrlArray["viewID"], dbName: encodeURI(UrlArray["dbName"]) }, function (result) {
                    if (result.RoleType == 1) {
                        $("#RoleType1").attr("checked", "checked");
                        $("#DateType").val(result.DateType);
                    }
                    else {
                        $("#RoleType2").attr("checked", "checked");
                        $("#DateType").hide();
                    }
                    $("#IncrementNum").val(result.IncrementNum);
                });
            }
            $.post("/SystemConfiguration/GetSourceColumnList/", { systemID: UrlArray["systemID"], viewID: UrlArray["viewID"], type: type, dbType: dbType, dbName: encodeURI(UrlArray["dbName"]) }, function (result) {
                if (result.Success) {
                    $("#selectView_3 tr:not(:first)").empty();
                    $("#selectView_3").append(result.Data);
                    $("#selectView_3 :radio").bind("click", function () {
                        if ($(this).attr("class").indexOf("checked") > -1) {
                            $(this).removeAttr("checked");
                            $(this).removeClass("checked");
                        }
                        else
                            $(this).addClass("checked");
                    });
                    $.post("/SystemConfiguration/GetAllColumnTypeList", function (res) {
                        $(".columnTypeList").append("<option value=''>-请选择-</option>");
                        $(".columnTypeList").append(res);
                    });
                } else
                    $(".msg").html(result.Message);
                var tableMode = $("input[name='TableMode']:checked").val();
                if (tableMode == "1" && dbType != "2") {
                    $("input[name='Incremental']").removeAttr("checked");
                    $("input[name='Incremental']").attr("disabled", "disabled");
                    $("input[name='ColumnIsBigDataTag']").removeAttr("checked");
                    $("input[name='ColumnIsBigDataTag']").attr("disabled", "disabled");
                    $("input[name='BigDataTagValue']").attr("disabled", "disabled");
                }
                $(".loading").hide();
            });

        }

        function SaveViewColumn(isauto) {
            var num = $("#selectView_3 tr").length;
            var displayName = $.trim($("#DisplayName").val());
            if (displayName == "") {
                alert("请输入显示名称.");
                return;
            }
            var viewChnName = $("#ViewChnName").val();
            var tableMode = $("input[name='TableMode']:checked").val();

            var RoleType = $("input[name='RoleType']:checked").val();

            if (tableMode != "1") {
                if ($("input[name='Incremental']:checked").length == 0) {
                    $(".loading").hide();
                    alert("选择增量模型时，必须指定增量字段.");
                    return;
                }
                if (UrlArray["DBType"] == "2" && $.trim($("#IncrementNum").val()) == "" || $.trim($("#IncrementNum").val()) == "0") {
                    $(".loading").hide();
                    alert("选择增量模型时，必须指定增量规则.");
                    return;
                }
            }

            var keyFlg = false;
            if (UrlArray["DBType"] != "2") {
                $(".PK").each(function (i, d) {
                    if (d.checked && $(".Extract" + i)[0].checked)
                        keyFlg = true;
                });
                if (!keyFlg) {
                    alert("请至少选一个字段作为主键");
                    $(".loading").hide();
                    return false;
                }
            } else
                keyFlg = true;
            var viewID = UrlArray["viewID"];
            var postArrData = new Array();
            $("#selectView_3 tr").each(function (i, d) {
                if (i > 0) {
                    var trArr = jv.GetDivArray($(this));
                    if (trArr.Extract == "1" && $.trim(trArr.ColumnType) == "") {
                        alert("提取字段的字段类型不能为空！");
                        $(".loading").hide();
                        keyFlg = false;
                        return;
                    }
                    if (trArr.Extract == "1")
                        postArrData.push(trArr);
                }
            });
            if (keyFlg) {
                if ('@Model.IsFile' == '1') {
                    tableMode = 2;
                }
                $(".loading").show();
                $.post("/SystemConfiguration/SaveSoureView", {
                    viewID: viewID,
                    displayName: displayName,
                    viewChnName: viewChnName,
                    tableMode: tableMode,
                    BigDataTag: $.trim($("#BigDataTag").val()),
                    query: $.toJSON(postArrData),
                    init: getArrValue(".txt_Init", "&"),
                    isauto: isauto,
                    RoleType: RoleType,
                    IncrementNum: $.trim($("#IncrementNum").val()),
                    DateType: $("#DateType").val(),
                    dbName: encodeURI(UrlArray["dbName"])
                }, function (result) {
                    $(".loading").hide();
                    switch (result.Data) {
                        case "1":
                            alert("保存成功！");
                            break;
                        default:
                            if (result.Message == null || result.Message == "") {
                                alert("保存失败");
                            }
                            else {
                                alert(result.Message);
                            }
                            break;
                    }
                });
            }
        }
        function selectAllCol() {
            var selector = $(".Extract");
            if ($(".selectAll").is(':checked')) {
                selector.each(function (i, d) {
                    if ($(this).attr("disabled") != "disabled") {
                        $(this).attr("checked", "checked");
                        d.checked = true;
                    }
                });
            }
            else {
                selector.removeAttr("checked");
            }
        }
        var viewsdb = true;
        function tab(v) {
            if (v == 1) {
                $("#tab1").show();
                $("#tab2").hide();
                $("#dv1").removeClass();
                $("#dv2").removeClass();
                $("#dv1").addClass("tb1");
                $("#dv2").addClass("tb2");
            } else {
                if (viewsdb) {
                    initSourceTableData();
                    viewsdb = false;
                }
                $("#tab1").hide();
                $("#tab2").show();
                $("#dv1").removeClass();
                $("#dv2").removeClass();
                $("#dv1").addClass("tb2");
                $("#dv2").addClass("tb1");
            }
        }
        var viewhead = true;
        function initSourceTableData(sortField, sortBy) {
            $(".loading2").show();
            $.post("/GetDate/GetSourceTableData/", { systemID: UrlArray["systemID"], DBID: DBID, viewID: UrlArray["viewID"], dbName: encodeURI(UrlArray["dbName"]), order: sortField, sort: sortBy, query: query }, function (result) {
                if (result.Success) {
                    $("#divSoutceTableData").html(result.Data);
                    if (viewhead) {
                        $("#sarea").html(result.Head);
                        viewhead = false;
                    }
                }
                else {
                    $(".msg2").html("读取源系统数据错误");
                }
                $(".loading2").hide();
            });
        }
        var query;
        function sreach() {
            var sr = [];
            $("#sarea tr").each(function (i, d) {
                var nm = d.id;
                if ($(".sel_" + nm).val() == "") return;
                var arr = new Array();
                arr.push(nm);
                arr.push($(".sel_" + nm).val());
                arr.push($.trim($(".txt_" + nm).val()));
                arr.push($(".ctp_" + nm).val());
                sr.push(arr);
            });
            $('#ViewSreach').hide(); $('#DIVMask').hide();
            query = $.toJSON(sr);
            initSourceTableData("", "");
        }
        function selShiftDesc(v) {
            var selectedValue = $(".Select" + v).val();
            $(".type" + v).val($(".Select" + v).find("option:selected").text());
            var extColType = "binary,varbinary,geography,geometry,hierarchyid,image,text,ntext,xml,timestamp";
            var arrColType = extColType.split(",");
            if (arrColType.indexOf(selectedValue) > -1) {
                $(".Extract" + v).removeAttr("checked");
                $(".PK" + v).removeAttr("checked");
                $(".Incremental" + v).removeAttr("checked");
                $(".Name" + v).removeAttr("checked");

                $(".Extract" + v).attr("disabled", "disabled");
                $(".PK" + v).attr("disabled", "disabled");
                $(".Incremental" + v).attr("disabled", "disabled");
                $(".Name" + v).attr("disabled", "disabled");

            } else {
                $(".Extract" + v).removeAttr("disabled");
                $(".PK" + v).removeAttr("disabled");
                $(".Incremental" + v).removeAttr("disabled");
                $(".Name" + v).removeAttr("disabled");
            }
        }
        function GetDBList() {
            showModalMany('GetDBItem', '/SystemConfiguration/GetDBList?systemID=' + UrlArray["systemID"] + "&dbName=" + encodeURI(UrlArray["dbName"]), '750', '465', function (res) {
                DBID = res[0].GUID;
                $("#txt_Host").val(res[0].Host + "," + res[0].Description);
            });
        }
    </script>
    <script type="text/javascript">
        function getDateType(v) {
            if (v == 1)
                $("#DateType").show();
            else
                $("#DateType").hide();
        }
        function EditInit() {
            $('#ViewInit').show();
            $('#DIVMask').show();
        }
        function closeInit() {
            $('#ViewInit').hide();
            $('#DIVMask').hide();
        }
        function saveInit() {
            if ($.trim($("#IncrementNum").val()) == "") {
                alert("值不能为空。");
            } else {
                $('#ViewInit').hide();
                $('#DIVMask').hide();
            }
        }
    </script>
}
<div style="margin: 5px; height: 500px; padding-right: 5px;">
    <table class="searchArea" width="100%" cellpadding="0" cellspacing="0">
        <tr>
            <td width="160">
                <div id="dv1" class='tb1' onclick="tab(1)">
                    配置源系统表字段信息</div>
            </td>
            <td>
                <div id="dv2" class='tb2' onclick="tab(2)">
                    预览数据源信息</div>
            </td>
            <td width="160">
            </td>
        </tr>
    </table>
    <div id="tab1">
        <table width="100%" class="searchArea">
            <tr>
                <td style="width: 100px; text-align: right;">
                    @Html.HiddenFor(model => model.Guid)
                    @Html.HiddenFor(model => model.SourceSystemID)
                    <input type="hidden" id="hid_ViewID" />
                    <span class="red">*</span>显示名称：
                </td>
                <td>
                    @Html.TextBoxFor(model => model.DisplayName, new { style = "width:350px" })
                </td>
                <td align="right" valign="top">
                    <input type="button" class="button" onclick="ReloadViewColumn()" value="刷新" />
                    <input type="button" class="button" onclick="SaveViewColumn('0')" value="保存" />
                </td>
            </tr>
            <tr>
                <td align="right">
                    表说明：
                </td>
                <td>@Html.TextBoxFor(model => model.ViewChnName, new { style = "width:350px" })
                </td>
                <td>
                </td>
            </tr>
            <tr id="div_TableMode">
                <td align="right">
                    表模式：
                </td>
                <td>
                    @Html.RadioButtonFor(Model => Model.TableMode, 1, new { @id = "Statusradio0", @name = "Status", @checked = "checked" })@Html.Label("Statusradio0", "全量模式")
                    @Html.RadioButtonFor(Model => Model.TableMode, 2, new { @id = "Statusradio1", @name = "Status" })@Html.Label("Statusradio1", "增量模式（升序）")
                    @Html.RadioButtonFor(Model => Model.TableMode, 3, new { @id = "Statusradio2", @name = "Status" })@Html.Label("Statusradio2", "增量模式（降序）")
                    <span class="isBigInit">
                        <input type="button" style="width: 117px" value="增量规则" onclick="EditInit();" />
                    </span>
                </td>
                <td>
                </td>
            </tr>
        </table>
        <div style="height: 350px; overflow-y: scroll;">
            <table class="listTitle">
                <tr>
                    <td width="15">
                        <img src="/images/edit.png" />
                    </td>
                    <td width="300">
                        源系统表字段列表
                    </td>
                    <td align="right">
                    </td>
                </tr>
            </table>
            <table id="selectView_3" width="778px" class="pagTable" cellpadding="0" cellspacing="0">
                <tr class='pagTablehd'>
                    <td width='20'>
                        <input type='checkbox' onclick='selectAllCol()' class="selectAll" />
                    </td>
                    <td style='width: 250px;'>
                        字段名称
                    </td>
                    <td style='width: 120px;'>
                        字段类型
                    </td>
                    <td style='width: 40px; text-align: center'>
                        主键
                    </td>
                    <td class="isfileDB" style='width: 60px; text-align: center'>
                        增量字段
                    </td>
                    <td class="isfileDB isBigInit" style='width: 80px; text-align: center'>
                        标识字段
                    </td>
                    <td>
                        说明
                    </td>
                </tr>
            </table>
            <div class="loading">
                <img src="/Images/tree_loading.gif" />
            </div>
            <div class="msg">
            </div>
        </div>
    </div>
    <div id="tab2" style="display: none;">
        <table id="tbSoutceTableData1" class="pagTable" cellpadding="0" cellspacing="0" style="background-image: /images/tbdata.jpg;">
        </table>
        <table class="listTitle">
            <tbody>
                <tr>
                    <td width="15">
                        <img src="/images/filesave.png" />
                    </td>
                    <td width="300">
                        预览数据源信息
                    </td>
                    <td align="right">
                        <input type="button" class="button" value="搜索" onclick="$('#ViewSreach').show();$('#DIVMask').show();" />
                    </td>
                </tr>
            </tbody>
        </table>
        <div id="divSoutceTableData" style="height: 400px; overflow-y: scroll;">
            <div class="msg2">
            </div>
            <div class="loading2">
                <img src="/Images/tree_loading.gif" />
            </div>
        </div>
        <div id="ViewSreach">
            <table class="listTitle">
                <tbody>
                    <tr>
                        <td width="15">
                            <img src="/images/search.png">
                        </td>
                        <td width="260">
                            搜索数据源信息
                        </td>
                        <td>
                            <input type="button" class="button" value="提交" onclick="sreach();" />
                            <input type="button" class="button" value="关闭" onclick="$('#ViewSreach').hide();$('#DIVMask').hide();" />
                        </td>
                    </tr>
                </tbody>
            </table>
            <div class="dbItem">
                选择连接数据库：<input type="text" id="txt_Host" style="width: 285px;" /><input type="button"
                    class="button" value="选择" onclick="GetDBList();" />
            </div>
            <div id="sarea">
            </div>
        </div>
    </div>
</div>
<div id="ViewInit">
    <table class="listTitle">
        <tbody>
            <tr>
                <td width="15">
                    <img src="/images/edit.png">
                </td>
                <td width="260">
                    增量规则
                </td>
                <td align="right">
                    <input type="button" class="button" value="确定" onclick="saveInit();" />
                    <input type="button" class="button" value="关闭" onclick="closeInit();" />
                </td>
            </tr>
        </tbody>
    </table>
    <div class="borderInit">
        <table width="100%" class="table_bg" cellpadding="0" cellspacing="0">
            <tr>
                <td width="80" align="right">
                    规则类型：
                </td>
                <td>
                    <input type="radio" name="RoleType" id="RoleType1" value="1" checked="checked" onclick="getDateType(1)" />日期
                    <input type="radio" name="RoleType" id="RoleType2" value="2" onclick="getDateType(0)" />数字
                </td>
            </tr>
            <tr>
                <td align="right">
                    值：
                </td>
                <td>
                    <input type="text" id="IncrementNum" maxlength="7" onkeyup="this.value=checkNumEmpty(this.value)" />
                    <select id="DateType">
                        <option value="1">天</option>
                        <option value="2">月</option>
                        <option value="3">年</option>
                    </select>
                </td>
            </tr>
        </table>
    </div>
</div>
<div id="DIVMask">
</div>
